package com.organization

class EmployeeService {

    def commonService

    def getEmployeeInfo(empId){
        Employee employee = Employee.get(commonService.nodeIdToDbId(empId))
        if (employee){
            def result = commonService.goodResponse
            def exclude = ['class', 'metaClass', 'constraints', 'belongsTo', 'hasMany', 'project', 'log', 'errors']
            employee.properties.grep({ !exclude.contains(it.key) }).each { result.putAt(it.key,it.value) }
            return result;
        }
        return commonService.badResponse
    }

    def updateEmployeeInfo(params){
        Employee employee = Employee.get(commonService.nodeIdToDbId(params.nodeId))
        if (employee){
            employee.lastName = params.lastName
            employee.firstName = params.firstName
            employee.workPosition = params.workPosition
            employee.hireDate = commonService.convertDateFromJson(params.hireDate)
            if (params.containsKey('extensionPhone'))
                employee.extensionPhone = params.extensionPhone
            if (params.containsKey('mobilePhone'))
                employee.mobilePhone = params.mobilePhone
            if (params.containsKey('homePhone'))
                employee.homePhone = params.homePhone
            if (params.containsKey('email'))
                employee.email = params.email
            if (params.containsKey('addressStreetCity'))
                employee.addressStreetCity = params.addressStreetCity
            if (params.containsKey('addressHouse'))
                employee.addressHouse = params.addressHouse
            if (params.containsKey('addressAaprtment'))
                employee.addressApartment = params.addressApartment
            if (params.containsKey('birthDate'))
                employee.birthDate = commonService.convertDateFromJson(params.birthDate)
            if (params.containsKey('gender'))
                employee.gender = params.gender
            if (employee.validate()){
                employee.save()
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('newName',employee.toString())
                response.putAt('nodeId',params.nodeId)
                return response
            }
            else{
                return commonService.parseErrorsFromDomain(employee.errors)
            }
        }
        return commonService.badResponse
    }

    def addEmployee(params){
        Project proj = Project.get(commonService.nodeIdToDbId(params.parentNodeId))
        if (proj){
            Employee employee = new Employee(
                    project: proj,
                    lastName: params.lastName,
                    firstName: params.firstName,
                    workPosition: params.workPosition,
                    extensionPhone: params.extensionPhone,
                    mobilePhone: params.mobilePhone,
                    homePhone: params.homePhone,
                    email: params.email,
                    addressStreetCity: params.addressStreetCity,
                    addressHouse: params.addressHouse,
                    addressApartment: params.addressApartment,
                    gender: params.gender,
                    hireDate: commonService.convertDateFromJson(params.hireDate),
                    birthDate: commonService.convertDateFromJson(params.birthDate)
            );
            if (employee.save()){
                employee.hireDate = commonService.convertDateFromJson(params.hireDate)
                employee.birthDate = commonService.convertDateFromJson(params.birthDate)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('nodeId',commonService.emplNodeStr+employee.id.toString())
                response.putAt('parentNodeId',params.parentNodeId)
                return response
            }
            else{
                return commonService.parseErrorsFromDomain(employee.errors)
            }
        }
        return commonService.badResponse
    }

    def deleteEmployee(params){
        Employee employee = Employee.get(commonService.nodeIdToDbId(params.nodeId))
        if (employee){
            employee.delete()
            return commonService.goodResponse
        }
        return commonService.badResponse;
    }

}
